<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>array</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.JSON">
<link rel="up" href="../dom.html" title="Document Model">
<link rel="prev" href="string.html" title="string">
<link rel="next" href="object.html" title="object">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="string.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="object.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="json.dom.array"></a><a class="link" href="array.html" title="array">array</a>
</h3></div></div></div>
<p>
        A <a class="link" href="../ref/boost__json__value.html" title="value"><code class="computeroutput"><span class="identifier">value</span></code></a>
        stores an instance of <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a> as the underlying representation
        for a JSON array. Instances of the <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a> type function identically
        to a <a href="https://en.cppreference.com/w/cpp/container/vector" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span></code></a>
        of <a class="link" href="../ref/boost__json__value.html" title="value"><code class="computeroutput"><span class="identifier">value</span></code></a>.
        Additionally:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            All values inserted into the container will use the same <a class="link" href="../ref/boost__json__storage_ptr.html" title="storage_ptr"><code class="computeroutput"><span class="identifier">storage_ptr</span></code></a> as the container
            itself.
          </li></ul></div>
<p>
        An empty array may be constructed without incurring any memory allocations
        using the default memory resource. A <a class="link" href="../ref/boost__json__storage_ptr.html" title="storage_ptr"><code class="computeroutput"><span class="identifier">storage_ptr</span></code></a> can also be explicitly
        specified:
      </p>
<pre class="programlisting"><span class="identifier">array</span> <span class="identifier">arr1</span><span class="special">;</span> <span class="comment">// empty array, uses the default memory resource</span>

<span class="identifier">array</span> <span class="identifier">arr2</span><span class="special">(</span> <span class="identifier">make_shared_resource</span><span class="special">&lt;</span><span class="identifier">monotonic_resource</span><span class="special">&gt;()</span> <span class="special">);</span> <span class="comment">// empty array, uses a counted monotonic resource</span>
</pre>
<p>
        Initializer lists can be used to construct objects with initial contents.
        These constructors may allocate memory and throw:
      </p>
<pre class="programlisting"><span class="identifier">array</span> <span class="identifier">arr</span><span class="special">(</span> <span class="special">{</span> <span class="string">"Hello"</span><span class="special">,</span> <span class="number">42</span><span class="special">,</span> <span class="keyword">true</span> <span class="special">}</span> <span class="special">);</span>
</pre>
<p>
        Alternatively, elements may be inserted after construction:
      </p>
<pre class="programlisting"><span class="identifier">array</span> <span class="identifier">arr</span><span class="special">;</span>

<span class="identifier">arr</span><span class="special">.</span><span class="identifier">emplace_back</span><span class="special">(</span> <span class="string">"Hello"</span> <span class="special">);</span>
<span class="identifier">arr</span><span class="special">.</span><span class="identifier">emplace_back</span><span class="special">(</span> <span class="number">42</span> <span class="special">);</span>
<span class="identifier">arr</span><span class="special">.</span><span class="identifier">emplace_back</span><span class="special">(</span> <span class="keyword">true</span> <span class="special">);</span>
</pre>
<p>
        Similar to its standard library counterpart, elements may be accessed directly
        by their 0-based index with bounds checking using <a class="link" href="../ref/boost__json__array/at/overload1.html" title="array::at (1 of 2 overloads)"><code class="computeroutput"><span class="identifier">at</span></code></a>, or without bounds checking using
        <a class="link" href="../ref/boost__json__array/operator_lb__rb_.html" title="array::operator[]"><code class="computeroutput"><span class="keyword">operator</span><span class="special">[]</span></code></a>:
      </p>
<pre class="programlisting"><span class="identifier">assert</span><span class="special">(</span> <span class="identifier">arr</span><span class="special">[</span><span class="number">0</span><span class="special">].</span><span class="identifier">as_string</span><span class="special">()</span> <span class="special">==</span> <span class="string">"Hello"</span> <span class="special">);</span>

<span class="comment">// The following line throws std::out_of_range, since the index is out of range</span>
<span class="identifier">arr</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span> <span class="number">3</span> <span class="special">)</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">;</span>
</pre>
<p>
        For the complete listing of all available member functions and nested types,
        see the reference page for <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a>.
      </p>
<h5>
<a name="json.dom.array.h0"></a>
        <span class="phrase"><a name="json.dom.array.formatted_output"></a></span><a class="link" href="array.html#json.dom.array.formatted_output">Formatted
        Output</a>
      </h5>
<p>
        When an <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a>
        is formatted to a <a href="https://en.cppreference.com/w/cpp/io/basic_ostream" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span></code></a>,
        the result is a valid JSON. That is, the array will be output with square
        brackets and the comma separated list of values, as per the JSON specification.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2019, 2020 Vinnie Falco<br>Copyright © 2020 Krystian Stasiowski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="string.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="object.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
